﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MigraDoc.DocumentObjectModel;
using Payroll.BusinessLogic.CustomerManager;
using Payroll.BusinessLogic.Utils;
using Payroll.Common.Utils;
using PayrollHR.Web;

namespace PayrollHR.Administration.Modules
{
    public partial class CustomerAddControl : BasePayrollAdministrationUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.BinCustomer();
            }
        }
        protected int CustomerID
        {
            get
            {
                return CommonHelper.QueryStringInt("CustomerId");
            }
        }
        protected void BinCustomer()
        {
            ctrlCustomerInfo.FillDropDown();
            if (this.CustomerID == 0)
                return;
            var customer = this.CustomerService.GetCustomerById(this.CustomerID);
            if (CommonHelper.IsNull(customer))
                return;
            ctrlCustomerInfo.BindCustomer(customer);
        }
        protected Customer Save()
        {
            Customer customer = ctrlCustomerInfo.GetCustomer();
            customer.CustomerId = this.CustomerService.Save(customer);
            return customer;
        }
        protected void SaveButton_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    this.Save();
                    Response.Redirect("Customers.aspx");
                }
                catch (Exception exc)
                {
                    ProcessException(exc);
                }
                finally {
                    SaveButton.Enabled = true;
                    SaveAndStayButton.Enabled = true;
                }
            }
        }
        protected void SaveAndStayButton_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    Customer customer = this.Save();
                    Response.Redirect(String.Format("CustomerAdd.aspx?CustomerId={0}", customer.CustomerId));
                }
                catch (Exception exc)
                {
                    ProcessException(exc);
                }
                finally
                {
                    SaveButton.Enabled = true;
                    SaveAndStayButton.Enabled = true;
                }
            }
        }
        
        protected void Cancel_Click(object sender, EventArgs e)
        {
            try {
                Response.Redirect(String.Format("CustomerAdd.aspx?CustomerId={0}", 0));
            }
            catch (Exception exc)
            {
                ProcessException(exc);
            }
        }
    }
}